package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author lyd
 * @since 2022-06-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    private IApUserService userService;
    @Autowired
    private MediaFeign mediaFeign;
    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //分页查询
        //构建分页条件 和  查询条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        //关键数据校验
        if (!StringUtils.isEmpty(dto.getStatus())){
            //根据状态查询
            query.eq(ApUserRealname::getStatus,dto.getStatus());
        }

        IPage<ApUserRealname> iPage = this.page(page, query);

        //响应给前端
        PageResponseResult<List<ApUserRealname>> result = new PageResponseResult<>(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
        return result;
    }

    /**
     *用户审核
     * status代表审核状态  1表示成功  0表示失败
     */
    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int status) {
        //根据id查询认证表
        ApUserRealname realname = this.getById(dto.getId());
        //审核不通过 状态为0的时候
        if(status == 0){
            //修改认证表状态为2  同时保存驳回原因
            realname.setStatus(2);
            realname.setReason(dto.getReason());
            this.updateById(realname);
            return ResponseResult.okResult();
        }

        //审核通过  状态为1的时候
        //查询用户的注册信息 并复制到自媒体账号中
        ApUser user = userService.getById(realname.getId());
        //创建自媒体账号
        WmUser wmUser = createWmUser(user);
        if (wmUser != null){
            //创建文章作者
            ApAuthor apAuthor =  createArcticle(user,wmUser);
            if (apAuthor != null){
                //修改用户认真表状态为9
                realname.setStatus(9);
                this.updateById(realname);
                return ResponseResult.okResult();
            }
        }
        return null;
    }

    /*
        创建文章作者
     */
    private ApAuthor createArcticle(ApUser user, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(user.getName());
        apAuthor.setUserId(user.getId());
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setType(2);
        ResponseResult<ApAuthor> result = articleFeign.saveApAuthor(apAuthor);
        if (result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            return apAuthor;
        }
        return null;
    }

    /**
     * 创建自媒体账号
     */
    private WmUser createWmUser(ApUser user) {
        WmUser wmUser = new WmUser();
        //复制user的基本信息
        BeanUtils.copyProperties(user,wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(user.getId());
        wmUser.setNickname(user.getName());
        //status表示wmUser的使用状态   0 暂时不可用            1 永久不可用            9 正常可用
        wmUser.setStatus(9);
        wmUser.setType(0);

        //发起远程调用
        ResponseResult<WmUser> result = mediaFeign.saveWmUser(wmUser);
        if (result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())){
            return wmUser;
        }
        return null;
    }
}
